package org.testis.data.parser;

import org.testis.data.*;
import org.testis.logging.AtofLogLevel;
import org.testis.logging.LogSenderService;
import org.testis.nicmodule.data.NicModuleDataObject;
import java.util.LinkedHashMap;


/**
 * Defines a class to parse data for network interface module
 * @author root
 */
public class NicModuleDop implements TestStepDop
{

    /**
     * Parsing data and creating a NicModule
     * Returns null if any required attribute not given
     * @param data
     * @return
     */
    public TestStepDataObject convert(TestStepDataObject data)
    {
        NicModuleDataObject dataObj =
            new NicModuleDataObject(data);
        LinkedHashMap<String, String> map = data.getDataToApply();
        LinkedHashMap<String, String> modulMap = data.getModuleDataToApply();
        String iFace = modulMap.get(AtofModuleParameterNames.NIC_INTERFACE);
        String modType = modulMap.get(AtofModuleParameterNames.NIC_MODTYPE);

        String ip = map.get(AtofModuleParameterNames.NIC_IP);
        String subnet = map.get(AtofModuleParameterNames.NIC_SUBNET);
        String dns = map.get(AtofModuleParameterNames.NIC_DNS);
        String gateway = map.get(AtofModuleParameterNames.NIC_GATEWAY);
        String enability = map.get(AtofModuleParameterNames.NIC_ENABILITY);
        String fromDhcp = map.get(AtofModuleParameterNames.NIC_FROM_DHCP);

        if ((iFace == null) || (modType == null)) {
            LogSenderService.putMessageOnQueue("NicModuleDop",
                                               AtofLogLevel.ERROR,
                                               "NicDataObject is not correctly " +
                                               AtofModuleParameterNames.NIC_INTERFACE +
                                               " or " +
                                               AtofModuleParameterNames.NIC_MODTYPE +
                                               " not given.",
                                               data.getMessageId());
            return null;
        }

        dataObj.setParamInterface(iFace);
        dataObj.setParamModule(modType);

        if (ip != null) {
            dataObj.setIP(ip);
        }
        if (subnet != null) {
            dataObj.setSubnet(subnet);
        }
        if (dns != null) {
            dataObj.setDNS(dns);
        }
        if (gateway != null) {
            dataObj.setGateway(gateway);
        }
        if (enability != null) {
            if (enability.equals("1") || enability.toLowerCase().equals("true")) {
                dataObj.setEnability(true);
            }
        }
        if (fromDhcp != null) {
            if (fromDhcp.equals("1") || fromDhcp.toLowerCase().equals("true")) {
                dataObj.setFromDhcp(true);
            }
        }

        return dataObj;

    }
}
